package com.fingard.dsp.bank.directbank.abc02.util;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import com.fingard.io.FileHelper;

import java.security.Key;

/**
 * 加解密工具类，提供标准的3DES实现
 * 
 * 
 */
public abstract class Des3 {
	private static final String Algorithm = "DESede"; // 定义 加密算法,可用
	// DES,DESede,Blowfish
	// keybyte为加密密钥，长度为24字节
	// src为被加密的数据缓冲区（源）
	public static byte[] encryptMode(byte[] keybyte, byte[] src) {
		try {
			// 生成密钥
			SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
			// 加密
			Cipher c1 = Cipher.getInstance(Algorithm);
			c1.init(Cipher.ENCRYPT_MODE, deskey);
			return c1.doFinal(src);
		} catch (java.security.NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		} catch (javax.crypto.NoSuchPaddingException e2) {
			e2.printStackTrace();
		} catch (Exception e3) {
			e3.printStackTrace();
		}
		return null;
	}

	// keybyte为加密密钥，长度为24字节
	// src为加密后的缓冲区
	public static byte[] decryptMode(byte[] keybyte, byte[] src) {
		try {
			// 生成密钥
			SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
			// 解密
			Cipher c1 = Cipher.getInstance(Algorithm);
			c1.init(Cipher.DECRYPT_MODE, deskey);
			return c1.doFinal(src);
		} catch (java.security.NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		} catch (javax.crypto.NoSuchPaddingException e2) {
			e2.printStackTrace();
		} catch (Exception e3) {
			e3.printStackTrace();
		}
		return null;
	}

	/**
	 * 3DES加密，DESede/CBC/PKCS5Padding
	 * @param p_key 24位密钥
	 * @param p_iv 8位向量
	 * @param p_plainData 明文字节
	 * @return 密文字节
	 * @throws Exception
	 */
	public static byte[] encryptCBC(byte[] p_key, byte[] p_iv, byte[] p_plainData) throws Exception {
		Key deskey = null;
		DESedeKeySpec spec = new DESedeKeySpec(p_key);
		SecretKeyFactory keyFac = SecretKeyFactory.getInstance("DESede");
		deskey = keyFac.generateSecret(spec);

		Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
		IvParameterSpec ivps = new IvParameterSpec(p_iv);
		cipher.init(Cipher.ENCRYPT_MODE, deskey, ivps);
		byte[] cipherBytes = cipher.doFinal(p_plainData);
		return cipherBytes;
	}

	/**
	 * 3DES解密，DESede/CBC/PKCS5Padding
	 * @param p_key 24位密钥
	 * @param p_iv 8位向量
	 * @param p_cipherData 密文字节
	 * @return 明文字节
	 * @throws Exception
	 */
	public static byte[] decryptCBC(byte[] p_key, byte[] p_iv, byte[] p_cipherData) throws Exception {
		Key deskey = null;
		DESedeKeySpec spec = new DESedeKeySpec(p_key);
		SecretKeyFactory keyFac = SecretKeyFactory.getInstance("DESede");
		deskey = keyFac.generateSecret(spec);

		Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
		IvParameterSpec ivps = new IvParameterSpec(p_iv);
		cipher.init(Cipher.DECRYPT_MODE, deskey, ivps);

		byte[] plainBytes = cipher.doFinal(p_cipherData);
		return plainBytes;
	}


	public static void main(String[] args) throws Exception{
		String key = "2012PinganVitality075522";
		String iv = "01234567";
		System.out.println("this is key :"+key);

		byte[] encoded =FileHelper.readFileBytes("E:\\ftpserver\\data\\403101926898.txt");
		String str = new String(encoded,"ASCII");
		System.out.println(str);
		byte[] decoded = new BASE64Decoder().decodeBuffer(str);
		System.out.println(encoded.length);
		byte[] srcBytes = decryptCBC(key.getBytes("gbk"),iv.getBytes("gbk"),decoded );
		System.out.println("解密后的字符串:" + (new String(srcBytes,"gbk")));
//		String fileCont = FileHelper.readFileText("C:\\Users\\F\\Desktop\\201904010908农行加密\\030200671498--明文","gbk");
//		byte[] encoded = Des3.encryptCBC(key.getBytes("gbk"), iv.getBytes("gbk"),fileCont.toString().getBytes("gbk"));
////				FileHelper.writeFile(tmpFilePath, new String(encoded), getCharset());
//		byte[] resultBase =  new BASE64Encoder().encode(encoded).getBytes("ASCII");
//		FileHelper.writeFile("C:\\Users\\F\\Desktop\\201903291024加密文件（改）\\","030200697473--密文",resultBase);

	}
}
